package com.qf.data.filter;

import com.qf.common.model.DeviceCmd;
import com.qf.common.utils.JsonUtil;
import com.qf.data.service.DeviceCmdService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 千锋健哥
 */
@Component
@Slf4j
public class DeviceCmdFilter {

    @Autowired
    private DeviceCmdService deviceCmdService;

    @RabbitHandler
    @RabbitListener(queues = "qf.queue.value.cmd")
    public void messageHandler(Channel channel, Message message, DeviceCmd deviceCmd) {
        try {
            log.info("[center-data数据中心]获取指令日志为:" + JsonUtil.toJsonString(deviceCmd));
            //调用service保存设备指令日志
            deviceCmdService.saveCmdLog(deviceCmd);
            //手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
